package controller;

import java.io.IOException;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.OpinionThatCounts;

/**
 * Servlet implementation class LoginController
 */
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		OpinionThatCounts otc=new OpinionThatCounts();
		RequestDispatcher rd = null;
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		int res = otc.loginValidate(username, password);
		if(res>0)
		{
			HttpSession hs = request.getSession(true);
			hs.setAttribute("username", username);
			//hs.setMaxInactiveInterval(10);
			request.setAttribute("username", username);
			rd=request.getRequestDispatcher("index.jsp");
			rd.forward(request, response);
		}
		else
		{
			request.setAttribute("errorMessage", "images/invalid.png");
			rd=request.getRequestDispatcher("test.jsp");
			rd.forward(request, response);
		}

	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, 
	 * 							HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, 
							HttpServletResponse response) 
							throws ServletException, IOException {
		doPost(request, response);
	}
}
